<template>
    <view class="content">
        <view class="padding-lr flex align-center min-width-0" v-for="(item, index) in list" :key="index" 
             @click="$gTo(`/mine/pages/chat/chat?otherId=${item.userProfile.userID}&fromList=${1}&selfId=${selfId}`)">
            <image class="user-img margin-right" v-if="item.userProfile.avatar" :src="item.userProfile.avatar"></image>
            <image class="user-img margin-right" v-else :src="IMAGEURL + 'static/icon/avatar.png'"></image>
            <view class="padding-tb border1 flex-sub min-width-0">
                <view class="flex align-center justify-between">
                    <view class="text-xl text-bold u-flex-1">{{item.userProfile.nick}}</view>
                    <!-- <view class="text-sm text-gray">{{item.create_time}}</view> -->
                </view>
                <view class="u-flex u-row-right u-col-center u-padding-top-15">
                    <view class="pinglun text-cut text-gray">{{item.lastMessage.messageForShow}}</view>
                    <!-- <view v-if="item.unread > 0" class="spot"></view> -->
                </view>
            </view>
        </view>
        
        <view style="height: 94rpx;padding-top: 30rpx;">
            <u-loadmore :status="status" v-if="status != 'loadmore'" />
        </view>
    </view>
</template>

<script>
    import { IMAGEURL } from '@/static/js/common.js';
    // import TIM from 'tim-wx-sdk';
    // import TIMUploadPlugin from 'tim-upload-plugin';
    import { genTestUserSig } from '@/debug/GenerateTestUserSig.js';
    
    // let options = {
    //     SDKAppID: 1600017866 // 接入时需要将 0 替换为您的云通信应用的 SDKAppID
    // };
    // let tim = TIM.create(options); // SDK 实例通常用 tim 表示
    // tim.setLogLevel(1);
    // tim.registerPlugin({
    //     'tim-upload-plugin': TIMUploadPlugin
    // });
    // console.log('tim',tim)
    export default {
        data() {
            return {
                IMAGEURL,
                selfId: '',//自己id
                otherId: '',//对方id
                list: [],
                status: 'loadmore', //是否显示没有更多了
            };
        },
        onLoad(option) {
            if (option.selfId) {
                this.selfId = option.selfId
            }
            if (option.otherId) {
                this.otherId = option.otherId
            }
            // this.selfId = uni.getStorageSync('userId')
            
            // if (!this.$getSync('IMLogin')) {
            //     this.imLogin()
            // } else {
            //     this.getConversationList()
            // }
            
            this.imLogin()
        },
        onReachBottom() {
        	if(this.status == 'nomore'){return}
        	this.getConversationList()
        },
        methods: {
            // 调用函数登录 IM
            async imLogin() {
                const userID = this.selfId + ''
                const userSig = genTestUserSig(userID).userSig
                const promise = await this.$tim.login({
                    userID: userID,
                    userSig: userSig
                });
                if (promise.code) return false
                console.log('IM登陆成功imLogin', promise);
                if (promise.data.errorInfo == "success") {
                    this.$setSync('IMLogin', 1)
                    uni.showLoading({
                    	title: '加载中...'
                    });
                    uni.$on('SDK_READY_list', () => {
                        console.log('聊天列表')
                        this.getConversationList()
                        // this.$tim.on(this.$TIM.EVENT.CONVERSATION_LIST_UPDATED, this.getConversationList);
                    })
                } else {
                    uni.showLoading({
                    	title: '加载中...'
                    });
                    this.getConversationList()
                }
                
                // tim.setLogLevel(0); // 普通级别，日志量较多，接入时建议使用
                this.$tim.setLogLevel(1); // release级别，SDK 输出关键信息，生产环境时建议使用
                return true
            },
            
            // 获取全量的会话列表
            getConversationList() {
                let _this = this
                let promise = this.$tim.getConversationList();
                promise.then(function(imResponse) {
                    console.log('getConversationList',imResponse)
                    const conversationList = imResponse.data.conversationList; // 全量的会话列表，用该列表覆盖原有的会话列表
                    const isSyncCompleted = imResponse.data.isSyncCompleted; // 从云端同步会话列表是否完成(isSyncCompleted为true,表示拉取完)
                    
                    if (conversationList && conversationList.length > 0) {
                    	_this.list = _this.list.concat(conversationList)
                    	_this.status = 'loadmore'
                    } else {
                    	_this.status = 'nomore'
                    }
                }).catch(function(imError) {
                    console.warn('getConversationList error:', imError); // 获取会话列表失败的相关信息
                });
                _this.$closeLoading()
            },
        }
    };
</script>

<style lang="scss">
    page{
        background-color: #f5f5f5;
    }
    .pinglun{
        width: 76vw;
    }
    .spot{
        width: 14rpx;
        height: 14rpx;
        border-radius: 50%;
        background-color: #ff5b5c;
    }
    .border1 {
        border-bottom: 1rpx solid #eaebf0;
    }
    .user-img{
        width: 90rpx;
        height: 90rpx;
        border-radius: 50%;
    }

</style>
